package cn.edu.cug.cs.gtl.renders.openinventor.meshes;

import cn.edu.cug.cs.gtl.apis.render.basics.Box3f;
import cn.edu.cug.cs.gtl.apis.render.basics.Vec3f;
import cn.edu.cug.cs.gtl.apis.render.meshes.Cell;
import cn.edu.cug.cs.gtl.renders.openinventor.basics.Box3fImpl;
import cn.edu.cug.cs.gtl.renders.openinventor.basics.Vec3fImpl;
import com.openinventor.inventor.SbVec3f;
import com.openinventor.meshviz.data.PbCell;
import com.openinventor.meshviz.data.PbMesh;

public class CellImpl implements Cell {
    private PbCell nativeObject = null;
    public CellImpl(PbCell v){
        this.nativeObject = v;
    }
    @Override
    public Object getNativeObject() {
        return nativeObject;
    }

    @Override
    public boolean attach(Object nativeObject) {
        if(nativeObject instanceof  PbCell){
            this.nativeObject = (PbCell) nativeObject;
            return true;
        }
        return false;
    }

    @Override
    public Object detach() {
        Object r = this.nativeObject;
        this.nativeObject=null;
        return r;
    }


    /**
     * Gets the area of a cell.
     *
     * @return
     */
    @Override
    public float getArea() {
        return nativeObject.getArea();
    }

    /**
     * Gets the cell's bounding box.
     *
     * @return
     */
    @Override
    public Box3f getBoundingBox() {
        return new Box3fImpl(nativeObject.getBoundingBox());
    }

    /**
     * Gets the cell's bounding box.
     *
     * @return
     */
    @Override
    public float[] getBounds() {
        return nativeObject.getBounds();
    }

    /**
     * Gets the cell's geometric center.
     *
     * @return
     */
    @Override
    public Vec3f getCenter() {
        return new Vec3fImpl(nativeObject.getCenter());
    }

    /**
     * Gets the index of the cell in the mesh.
     *
     * @return
     */
    @Override
    public int getIndex() {
        return nativeObject.getIndex();
    }

    /**
     * Gets the length of the longest edge of the cell.
     *
     * @return
     */
    @Override
    public float getLongestEdgeLength() {
        return nativeObject.getLongestEdgeLength();
    }

    /**
     * Gets the coordinate of the nod (nod must be >=0 and < getNumNodes()).
     *
     * @param nod
     * @return
     */
    @Override
    public Vec3f getNodeCoord(int nod) {
        return new Vec3fImpl(nativeObject.getNodeCoord(nod));
    }

    /**
     * Gets the index (in the mesh) of a nod (nod must be >=0 and < getNumNodes()).
     *
     * @param nod
     * @return
     */
    @Override
    public int getNodeIndex(int nod) {
        return nativeObject.getNodeIndex(nod);
    }

    /**
     * Gets the number of edges.
     *
     * @return
     */
    @Override
    public int getNumEdges() {
        return nativeObject.getNumEdges();
    }

    /**
     * Gets the number of facets.
     *
     * @return
     */
    @Override
    public int getNumFacets() {
        return nativeObject.getNumFacets();
    }

    /**
     * Gets the number of nodes.
     *
     * @return
     */
    @Override
    public int getNumNodes() {
        return nativeObject.getNumNodes();
    }

    /**
     * Gets the scalar value of a point in this cell, given the point's weights.
     *
     * @param weight
     * @param s
     * @return
     */
    @Override
    public float getValue(float[] weight, float[] s) {
        return nativeObject.getValue(weight,s);
    }

    /**
     * Gets the vector value of a point in this cell, given the point's weights.
     *
     * @param weight
     * @param v
     * @return
     */
    @Override
    public Vec3f getValue(float[] weight, Vec3f[] v) {
        return new Vec3fImpl(nativeObject.getValue(weight,Vec3fImpl.convertArray(v)));
    }

    /**
     * Get the scalar value of a point in the cell.
     *
     * @param pcoord
     * @param s
     * @return
     */
    @Override
    public float getValue(Vec3f pcoord, float[] s) {
        return nativeObject.getValue((SbVec3f) pcoord.getNativeObject(),s);
    }

    /**
     * Get the vector value of a point in the cell.
     *
     * @param pcoord
     * @param v
     * @return
     */
    @Override
    public Vec3f getValue(Vec3f pcoord, Vec3f[] v) {
        return new Vec3fImpl(nativeObject.getValue((SbVec3f) pcoord.getNativeObject(),Vec3fImpl.convertArray(v)));
    }

    /**
     * Gets the volume of the cell.
     *
     * @return
     */
    @Override
    public float getVolume() {
        return nativeObject.getVolume();
    }

    /**
     * Returns 1 if the point is inside the cell, 0 otherwise.
     *
     * @param coord
     * @param tolerance
     * @return
     */
    @Override
    public Vec3f locatePoint(Vec3f coord, float tolerance) {
        return new Vec3fImpl(nativeObject.locatePoint((SbVec3f) coord.getNativeObject(),tolerance));
    }
}
